$().ready(function() {
	$.fn.hideErrors = function(){
		return this.each(function(){
			var element = $(this);
			element.live({
				keydown: clear,
				'input paste': clear	
			})
			function clear(){
				if(!element.val()=='') $('.userError',element.parent()).hide('slide');
			}
			});
	};
	$.fn.fieldClear = function(){
		return this.each(function(){
			var element = $("textarea,input", this);
			var label = $("label", this);
		
			element.live({
				focus: function(){
					label.animate({
						opacity:0.4
					},200)
				},
				blur : function(){
					label.animate({
						opacity:1
					},200)
				},
				keyup: key,
				keydown: key,
				'input paste': key	
			})
			function key(){
				if(element.val()=='') label.removeClass("hide");
				else label.addClass("hide");
			}
		})
	};
	$('.main-page').livequery(function() {
		$(function(){
			$('.field').fieldClear();
			$('.mandatory').hideErrors();
		});
	$('#fast-order-form').submit(function() {
		$objForm = $(this);
		$.ajax({
			dataType: 'json',
			url: W_AJAX + 'order/check_user',
			type: 'POST',
			success: function(data) { 
				if(data.userType == 'true' || data.login == 'false') {
					if($objForm.fullValidate()){
						if(data.login == 'true') content = $('#login-adding');
						else content = $('#no-login-adding');
								jQuery.fancybox({
										'content': $('#full-order'),
										afterLoad: function() {
											$('#Order_order_name',$('#full-order')).val($('#Order_order_name',$objForm).val());
											$('#Order_description',$('#full-order')).val($('#Order_description',$objForm).val());
											$('#city',$('#full-order')).html($('#Order_city',$objForm).val());
											content.show();
										}	
								});
								$(function(){
									$('.mandatory').hideErrors();
								});
					}
				}
				else jQuery.fancybox({
						'autoSize' : false,
						'content': 'Только пользователи со статусом "Заказчик" могут оставлять заявки на ремонт',
						'width' : 300,
						'height' : 100
					 });
			}	
		});
		return false;
	});
	
	$('#fast-order-form-full').live('submit',function() {
		if($(this).fullValidate()) {			
			$.ajax({
				dataType: 'json',
				url: W_AJAX + 'order/fast_add',
				type: 'POST',
				data: {
						order_name: $('#Order_order_name',this).val(),
						description: $('#Order_description',this).val(),
						city: $('#Order_city',$objForm).val(),
						phone: $('#Order_phone',this).val(),
						cost: $('#Order_cost',this).val(),
						first_name: $('#Order_first_name',this).val(),
						last_name: $('#Order_last_name',this).val(),
						pass: $('#Order_pass',this).val(),
						pass_confirmation: $('#Order_pass_confirmation',this).val(),
						e_mail: $('#Order_e_mail',this).val(),
						pass_confirmation: $('#Order_pass_confirmation',this).val(),
						kaptcha: $('#Order_kaptcha',this).val()
					},
				success: function(data) {
					if(data.result == 'OK') {
						url = W_ABSPATH + 'orders/' + data.orderId;
						location.href = url;
					}
					else if (data.error == 'mailDublicated') {
						$('#Order_e_mail').after('<div class="userError">Эта почта уже используется<div>');
					}
					else if (data.error == 'kaptchaError') {
						if (!$('.userError', '#kaptcha-right').length) {
							$('#Order_kaptcha').after('<div class="userError">Неверные данные<div>');
						}
					}
				}	
			});
		}
		return false;
	});
	$('.sub-but').click(function(event){
		$('#fast-order-form').submit();
		event.stopPropagation();
	});
	});
	
	$(".sub-but").mousedown(function() {
		$(this).css({'top' : '1px','box-shadow' : '1px 2px 5px rgba(0,0,0,0.5)'});
	});
	$("body").mouseup(function() {
		if($(".sub-but").css('top') != '0px') $(".sub-but").css({'top' : '0','box-shadow' : '2px 3px 6px rgba(0,0,0,0.5)'});
	});
});